var express = require("express");
var router = express.Router();
var { showType } = require("./tree");
var multiparty = require("multiparty");
var {
 commentModel,
} = require("../../model/model");

router.get("/commentList", async (req, res) => {
  let { btnText ,userId} = req.query;
  console.log(btnText);
  let ls =null;
  let total = 0;
  switch (btnText) {
    case "全部":
      ls = await commentModel.find({userId:userId}).populate("userId").populate("goodsId");
      total = await commentModel.countDocuments();
      res.send({
        code: 200,
          total,
        list: ls,
      });
      break;
    case "近期":
      ls = await commentModel
        .find({ userId: userId })
        .sort({ time: -1 })
        .populate("userId")
        .populate("goodsId")
        .populate("userId")
        .populate("goodsId");
      res.send({
        code: 200,
        list: ls,
      });
      break;
    case "好评":
       ls = await commentModel
         .find({ goodsRate: { $gte: 4 }, userId: userId })
         .populate("userId")
         .populate("goodsId");

      res.send({
        code: 200,
        list: ls,
      });
      break;
    case "中评":
      ls = await commentModel
        .find({ goodsRate: { $gte: 2, $lt: 4 } , userId: userId })
        .populate("userId")
        .populate("goodsId");

      res.send({
        code: 200,
        list: ls,
      });
      break;
    case "差评":
      ls = await commentModel
        .find({ goodsRate: { $lt: 2 } , userId: userId })
        .populate("userId")
        .populate("goodsId");

      res.send({
        code: 200,
        list: ls,
      });
      break; 
    case "有图":
      ls = await commentModel.find({ img: { $ne: [] } , userId: userId })
        .populate("userId")
        .populate("goodsId");

      res.send({
        code: 200,
        list: ls,
      })
      break;
    default:
      ls = await commentModel
        .find({ userId: userId })
        .populate("userId")
        .populate("userId")
        .populate("goodsId");

      res.send({
        code: 200,
        list: ls,
      });
      break;
  }
  console.log(ls);
  // if(btnText == '全部'){
  //     let ls = await commentModel.find();
  //     res.send(ls);
  // }
  // else if(btnText == '近期'){
  //     let ls = await commentModel.find().sort({time:-1});
  //     res.send(ls);

  // }else if(btnText == '好评'){
  //     let ls = await commentModel.find({commentRate:{$gte:4}});
  //     res.send(ls);
  // }else if(btnText == '中评'){
  //     let ls = await commentModel.find({commentRate:{$gte:2,$lt:4}});
  //     res.send(ls);

  // }else if(btnText == '差评'){
  //     let ls = await commentModel.find({commentRate:{$lt:2}});
  //     res.send(ls);
  // }else if(btnText == '有图'){
  //     let ls = await commentModel.find({img:{$ne:null}});
  //     res.send(ls);
  // }
  // let ls = await commentModel.find();
});


router.post('/addComment',(req,res)=>{
    console.log(req.body);
    commentModel.create(req.body)
    res.send({
      code: 200, 
      msg: "添加成功",
    });
})


router.post("/upload", async (req, res) => {
  let form = new multiparty.Form(); 
  form.uploadDir = "upload";

  form.parse(req, async (err, fields, file) => {

  
    if (err) {
      return res.status(400).send({ code: 400, message: "文件解析失败" });
    }

    try {
      const commentPath = "https://localhost:3000/" + file.file[0].path; // 获取头像文件路径 // 更新用户头像路径
        console.log(commentPath);
      return res.send({
        code: 200,
        message: "上传成功",
        data: commentPath,
      });
    } catch (error) {
      console.error("更新数据库失败:", error);
      return res.status(500).send({ code: 500, message: "服务器内部错误" });
    }
  });
});

module.exports = router;
